import Vuex from 'vuex'
import Vue from 'vue'
import * as types from './types'

Vue.use(Vuex);
export default new Vuex.Store({
  state: {
    user: {},
    token: null,
    title: '',


//        通知框
    showToast: false,
    toastMessage: '',

    //图片查看
    showImagePopup: false,
    displayedImage: null,

    feedbackTitle:'',
    feedbackContent:'',
    imageToUpload: []
  },
  mutations: {
    [types.LOGIN]: (state, data) => {
      localStorage.token = data;
      state.token = data;
    },
    [types.LOGOUT]: (state) => {
      localStorage.removeItem('token');
      localStorage.removeItem('userIdentity');
      localStorage.removeItem('isCommissary');
      localStorage.removeItem('userInfo');
      state.token = null
    },
    [types.TITLE]: (state, data) => {
      state.title = data;
    },
    showToast(state, message) {
      state.showToast = true
      console.log(state.showToast)
      state.toastMessage = message
      // 3秒钟后关闭
      setTimeout(() => {
        state.showToast = false
      }, 3000)
    },
    closeToast(state) {
      state.showToast = false
    },
    showImagePopup(state, imageSrc) {
      state.displayedImage = imageSrc
      state.showImagePopup = true
    },
    closeImagePopup(state) {
      state.showImagePopup = false
    },


    addImageToUpload(state, image) {
      let newImage = {image, url: URL.createObjectURL(image)}
      state.imageToUpload.push(newImage)
      return newImage
    },
    deleteImageToUpload(state,index){
      state.imageToUpload.splice(index,1)
    },
    updateFeedbackTitle(state,title){
      state.feedbackTitle = title
    },
    updateFeedbackContent(state,content){
      state.feedbackContent = content
    },
    initializeFeedback(state){
      console.log('OK')
      state.feedbackContent = ''
      state.feedbackTitle = ''
      state.imageToUpload.forEach(value => {URL.revokeObjectURL(value.url)})
      state.imageToUpload = []
    }
  },
  actions: {}
})
